# 计算器


from tkinter import ttk
from tkinter import *
import tkinter as tk
from tkinter import messagebox
#  pyinstaller -F manage.py --hidden-import tkinter --noconsole


class Application(Frame):
    """GUI application that manages a simple inventory"""
    def __init__(self, master):
        """Initialize the frame"""
        super(Application, self).__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        """Create widgets to get product information and to display results."""
        # self.dropdown = ttk.Combobox(root, values=["工资薪资所得", "劳动报酬所得", "偶然所得"])
        # self.dropdown.pack()
        # 设置默认值
        # self.dropdown.set("工资薪资所得")
        # # 绑定事件，当选择项改变时调用回调函数
        # self.combobox.bind("<<ComboboxSelected>>", self.on_select)

        # self.dropdown.grid(row=0, column=0, columnspan=4,sticky=EW)

        self.input_box = Entry(self)
        self.input_box.grid(row=1, column=0,columnspan=4,sticky=EW)
        self.result = Entry(self)
        self.result.config(state='readonly')
        self.result.grid(row=2, column=0,columnspan=4,sticky=EW,)



        btnText = (
            ('AC', '%', '删除',),
            (7, 8, 9,),
            (4, 5, 6,),
            (1, 2, 3,),
            (0, '.', '=',)
        )
        for row_num, row_text in enumerate(btnText):
            row_num += 1
            for col_num, col_text in enumerate(row_text):
                btn = Button(self, text=col_text, width=5)
                btn.config(command=lambda text=col_text: self.add_text(text))
                # if col_text == '0':
                #     btn.grid(row=row_num+2, column=col_num, columnspan=1, sticky=NSEW)
                # if col_text == '.':
                #     btn.grid(row=row_num+2, column=col_num+2,sticky=NSEW)
                if col_text == '=':
                    btn.grid(row=row_num+2, column=col_num,columnspan=2, sticky=NSEW)
                    btn.config(command=lambda: self.calculate())
                if col_text == 'AC':
                    btn.grid(row=row_num+2, column=col_num,sticky=NSEW)
                    btn.config(command=lambda: self.input_box.delete(0, END))

                if col_text == '删除':
                    btn.grid(row=row_num+2, column=col_num,sticky=NSEW)
                    btn.config(command=lambda: self.input_box.delete(len(self.input_box.get())-1))

                else:
                    btn.grid(row=row_num+2, column=col_num,sticky=NSEW)

    def add_text(self, text):
        """Add text to the end of the result entry"""
        current = self.input_box.get()
        self.input_box.delete(0, END)
        self.input_box.insert(0, str(current) + str(text))
        self.input_box.update()

    def calculate(self):
        """Calculate the result and display it"""
        # eval 函数用来计算字符串表达式的值
        content = self.input_box.get()
        # result = content.replace('÷', '/').replace('x', '*')
        result = self.calculate_fujian_income_tax(int(content))
        self.result.config(state='normal')
        self.result.delete(0, END)
        self.result.insert(0, result)
        self.result.update()
        self.result.config(state='readonly')

    def on_select(self, event):
        # 下拉列表框选择项改变时的回调函数
        selected_value = self.combobox.get()
    @staticmethod

    def calculate_fujian_income_tax(income, basic_deduction=5000, special_deductions=0, additional_deductions=0):
        """
        计算福建个人所得税

        :param income: 月收入（元）
        :param basic_deduction: 基本减除费用（元，默认5000元）
        :param special_deductions: 专项扣除（元，默认0元）
        :param additional_deductions: 专项附加扣除（元，默认0元）
        :return: 应纳税额（元）
        """
        # 计算应纳税所得额
        taxable_income = income - basic_deduction - special_deductions - additional_deductions

        # 如果应纳税所得额小于等于0，则无需缴税
        if taxable_income <= 0:
            return 0

        # 确定税率和速算扣除数
        tax_brackets = [
            (3000, 0.03, 0),
            (12000, 0.10, 210),
            (25000, 0.20, 1410),
            (35000, 0.25, 2660),
            (55000, 0.30, 4410),
            (80000, 0.35, 7160),
            (float('inf'), 0.45, 15160)
        ]

        for bracket in tax_brackets:
            if taxable_income <= bracket[0]:
                tax_rate = bracket[1]
                quick_deduction = bracket[2]
                break

        # 计算应纳税额
        tax_amount = taxable_income * tax_rate - quick_deduction

        return max(0, tax_amount)

    # 示例计算
    income = 15000
    special_deductions = 1000
    additional_deductions = 1000

    tax = calculate_fujian_income_tax(income, special_deductions=special_deductions,
                                      additional_deductions=additional_deductions)
    print(f"应纳税额: {tax:.2f}元")


if __name__ == '__main__':
    root = Tk()
    root.title("个税计算")
    root.geometry("220x200+500+250")
    app = Application(root)
    root.mainloop()
